package com.iwuke.dance.wallet.settings;

import android.view.View;
import android.widget.TextView;

import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.Bean;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.PhoneMaskUtil;
import com.iwuke.dance.util.SharedPreferencesUtil;
import com.iwuke.dance.util.ToastUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.independent_edit_text.IndependentEditTextUtil;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/10/9
 * 短信验证码
 */
public class SMSVerificationCodeActivity extends BaseActivity implements View.OnClickListener {


    private TitleBarView title_bar;
    private TextView tv_verification_method;
    private IndependentEditTextUtil independentEditTextUtil;
    private TextView tv_phone_number;

    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(SMSVerificationCodeActivity.this,networkEvent,title_bar);
    }

    @Override
    protected int getView() {
        return R.layout.activity_sms_verification_code;
    }

    @Override
    protected void init(View mView) {
        title_bar = findViewById(R.id.title_bar);
        //验证方式
        tv_verification_method = findViewById(R.id.tv_verification_method);
        //手机号
        tv_phone_number = findViewById(R.id.tv_phone_number);

        independentEditTextUtil = new IndependentEditTextUtil(this, mView);

        title_bar.setTitleText("修改支付密码");
        title_bar.setLeftOnClick(this);
        //验证方式
        tv_verification_method.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        //手机号
        String phoneNumber = SharedPreferencesUtil.getInstance().getPhoneNumber();
        tv_phone_number.setText(PhoneMaskUtil.maskPhoneNumber(phoneNumber));
        sendSMS();//发送验证码的请求

        independentEditTextUtil.setOnEditTextClick(new IndependentEditTextUtil.OnEditTextClick() {
            @Override
            public void onEditTextClick(String value) {
                //跳转设置新6位数密码页面
                //type: 3 换绑银行卡；4 绑定银行卡；5 重置支付密码；6 修改支付密码；7 钱包开通
                Map<String, Object> map = new HashMap<>();
                map.put("type",6);
                map.put("phoneNum",phoneNumber);
                map.put("verifyCode",value);

                PathUrl.verifySMS(map, new PathUrl.DataCallBack<Bean>() {
                    @Override
                    public void onSucess(Bean bean) {

                        //绑定本人身份信息
                        Util.setIntent(SMSVerificationCodeActivity.this,ChangePaymentPasswordActivity.class,"6");
                        finish();
                    }

                    @Override
                    public void onFail(String errorCode) {
                        ToastUtil.setToast(SMSVerificationCodeActivity.this,errorCode);
                    }
                });

            }

            @Override
            public void onResendTimeClick() {
                //发送验证码请求
                sendSMS();//发送验证码的请求
            }
        });
    }

    /**
     * @description 发送验证码的请求
     * @param //type: 3 换绑银行卡；4 绑定银行卡；5 重置支付密码；6 修改支付密码；7 钱包开通
     * @return
     * @author 魏从响
     * @time 2025/10/9 14:26
     */
    private void sendSMS(){
        Map<String, Object> map = new HashMap<>();
        map.put("type",6);
        map.put("phoneNum",SharedPreferencesUtil.getInstance().getPhoneNumber());
        PathUrl.sendSMS(map, new PathUrl.DataCallBack<Bean>() {
            @Override
            public void onSucess(Bean bean) {//429
                if (bean!=null){
                    independentEditTextUtil.resendTime((int) bean.id);
                }else {
                    independentEditTextUtil.startResendTimer();
                    ToastUtil.setToast(SMSVerificationCodeActivity.this,"发送成功");
                }
            }

            @Override
            public void onFail(String errorCode) {
                ToastUtil.setToast(SMSVerificationCodeActivity.this,errorCode);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            finish();
        }else if (v.getId()==R.id.tv_verification_method){//验证方式
            Util.setIntent(SMSVerificationCodeActivity.this, PaymentPasswordActivity.class);
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (independentEditTextUtil!=null){
            independentEditTextUtil.onDestroy();
        }
        if (independentEditTextUtil!=null){
            independentEditTextUtil=null;
        }
    }

}
